<?php
get_header(); ?>

<div class="container">
    <? while (have_posts()) : the_post(); ?>

    <?
    $custom = get_fields($post->ID);
    $currentPost = $post->ID;
    //var_dump($fields);
    ?>

    <div>
        <h1><? the_title(); ?></h1>

        <div class="row-fluid">
            <div class="span8">

                <? printImage($custom['image_1'], $custom['name'], 'pitch-main') ?>

                <div class="row-fluid" style="padding-top: 10px;">
                    <div class="span12">
                        <div class="card grey">
                            <h3 class="title">Venue Information</h3>

                            <div style="background:white; padding:19px; margin:0 -19px;">
                                <form class="form-horizontal" style="margin-bottom: 0px;">
                                    <?
                                    $items = array(
                                        "name" => "Name",
                                        "address" => "Address",
                                        "nearest_station" => "Nearest Station(s)",
                                        "changing_rooms" => "Changing Rooms",
                                        "showers" => "Showers"
                                    );
                                    ?>
                                    <? foreach ($items as $key => $value) { ?>
                                    <div class="control-group"><strong style="float:left;margin-top:0"><? echo $value ?></strong>
                                        <div id="<? echo $key ?>" class="controls" style="margin-left: 130px;"><? echo $custom[$key] ?></div>
                                    </div>
                                    <? } ?>


                                    <div class="control-group" style="margin-bottom: 0px;"><strong style="float:left;margin-top:0">Pitches</strong>

                                        <div class="controls" style="margin-left: 130px;">
                                            <?
                                            $pitch_query = new WP_Query(array('post_type' => 'pitch', 'showposts' => 100, 'meta_key' => 'venue', 'meta_value' => $post->ID, 'orderby' => 'title', 'order' => 'ASC'));
                                            while ($pitch_query->have_posts()) : $pitch_query->the_post();
                                                $pitchFields = get_fields($pitch_query->post->ID);
                                                ?>
                                                <a href="<? echo get_permalink($pitch_query->post->ID) ?>" class="btn btn-primary btn-mini" style="margin-right: 10px;font-weight: 800"><? echo $pitchFields['pid']; ?> &raquo;</a>
                                                <?
                                            endwhile;
                                            ?>
                                        </div>
                                    </div>
                                </form>
                            </div>


                        </div>
                    </div>
                </div>
            </div>

            <div class="span4">


                <div class="card grey">
                    <h3 class="title">Location</h3>

                    <div class="card-image">
                        <div id="map_canvas" style="width: 100%; height: 215px"></div>
                    </div>
                    <?php the_field('address'); ?>
                </div>
                <div class="card grey">
                    <h3 class="title">Available Slots</h3>
                    <select id="booking-select" style="width:100%">
                        <?
                        for ($a = 0; $a <= 13; $a++) {
                            echo '<option value="'
                                . date('d-m-Y', strtotime('+' . $a . ' day'))
                                . '">'
                                . date('l, j F Y', strtotime('+' . $a . ' day'))
                                . '</option>';
                        }
                        ?>
                    </select>

                    <table id="booking-table" class="table table-striped table-condensed table-bordered" style="text-align: center;margin-bottom: 0;">
                        <thead>
                        <tr>
                            <th>Pitch</th>
                            <th>Slot</th>
                            <th>Price</th>
                            <th></th>
                        </tr>
                        </thead>
                        <tbody>
                            <?

                            for ($a = 0; $a <= 13; $a++) {
                                $q = new WP_Query(
                                    array(
                                        'post_type' => 'slot',
                                        'posts_per_page' => 999,
                                        'meta_key' => 'start_time',
                                        'orderby' => 'meta_value_num',
                                        'order' => ASC
                                    )
                                );

                                while ($q->have_posts()) : $q->the_post();
                                    $qid = $q->post->ID;
                                    if (getVenueID($qid) == $currentPost) {
                                        if (in_array(date('l', strtotime('+' . $a . ' day')), get_field('day', $qid))) {
                                            ?>
                                        <tr style="white-space: nowrap" data-date="<? echo date('d-m-Y', strtotime('+' . $a . ' day')) ?>">
                                            <td><a href="<? echo get_permalink(getPitchID($qid)) ?>"><? echo get_field('pid', getPitchID($qid)) ?></a></td>
                                            <td><? echo get_field('start_time', $qid) . ' - ' . get_field('end_time', $qid) ?></td>
                                            <td><del style="color:#777"><? echo number_format(get_field('price', $qid), 2, '.', '')  ?></del> <span style="font-weight:700">&pound;<? echo number_format(get_field('price', $qid)*0.95, 2, '.', '') ?></span></td>
                                            <td>
                                                <form action="/confirm-booking/" method="post" style="margin:0">
                                                    <input type="hidden" name="form_pitch" value="<? echo getPitchID($qid) ?>">
                                                    <input type="hidden" name="form_slot" value="<? echo $qid ?>">
                                                    <input type="hidden" name="form_date" value="<? echo date('d-m-Y', strtotime('+' . $a . ' day')) ?>">
                                                    <input type="hidden" name="form_price" value="<? echo get_field('price', $qid)*0.95 ?>">

                                                    <?
                                                    $data = array(
                                                        "date" => date('Ymd', strtotime('+' . $a . ' day')),
                                                        "slot" => $qid
                                                    );

                                                    $response = doesBookingConflict($data);
                                                    if (!$response['conflict']) {
                                                        ?>
                                                        <input type="submit" class="btn btn-mini btn-primary" style="font-weight:800;white-space:nowrap;" value="Book &raquo;"/>
                                                        <?
                                                    } else {
                                                        ?>
                                                        <input type="submit" disabled="disabled" class="btn btn-mini btn-inverse" style="font-weight:800;white-space:nowrap;" value="Booked!"/>
                                                        <?
                                                    }
                                                    ?>
                                                </form>
                                            </td>
                                        </tr>
                                            <?
                                        }
                                    }
                                endwhile;
                                wp_reset_postdata();
                            }

                            ?>
                        <tr class="none">
                            <td style="text-align: center;" colspan="4">No slots avaliable</td>
                        </tr>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>


    </div>

    <? endwhile; ?>
</div>

<?php get_footer(); ?>

<script type="text/javascript"
        src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB-k-7b00P-IEkcvnNmQekON1RL9SVPSQI&sensor=false"></script>
<script src="<?php echo get_template_directory_uri(); ?>/js/PITCHWISE.map.js"></script>

<script>
    $(function () {
        var options = {};
        var geoMap = new PITCHWISE.map(options);
        PW.pitch.init();
    });
</script>